package br.ufpe.cin.camid.remote.distribution.interceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import org.aopalliance.intercept.MethodInterceptor;


public class Interceptors 
{
	private SortedSet<OrderedInterceptor> interceptors;

	public Interceptors()
	{
		interceptors = new TreeSet<OrderedInterceptor> ();
	}
	
	public void register(MethodInterceptor interceptor)
	{
		int priority = 10;
		
		if(interceptor.getClass().isAnnotationPresent(InterceptorPriority.class))
		{
			InterceptorPriority config = interceptor.getClass().getAnnotation(InterceptorPriority.class);
			priority = config.priority();
			
		}
		interceptors.add(new OrderedInterceptor(interceptor, priority));
	}
	
	public List<MethodInterceptor> listInterceptors()
	{
		List<MethodInterceptor> list = new ArrayList<MethodInterceptor>();
		for(OrderedInterceptor orderedIntereceptor : interceptors)
		{
			list.add(orderedIntereceptor.getInterceptor());
		}
		return list;
	}
	
}
